

<template>
  <div>
    <van-form @submit="onSubmit">
     <van-field
        v-model="userOj.login_id" label="手机号码" 
        :rules="[{ required: true, message: '请填写电话' }]"/>

      <van-dropdown-menu>
        <van-dropdown-item v-model="role" :options="roles" />
        <van-dropdown-item v-model="state" :options="states" />
      </van-dropdown-menu>

      <van-field
        v-model="userOj.name"  label="称呼"
        :rules="[{ required: true, message: '请填写称呼' }]"/>
      <van-field
        v-model="userOj.job" label="职业" 
        :rules="[{ required: true, message: '请填写职业' }]"/>
      <van-field
        v-model="userOj.memo"  label="其他说明" 
        :rules="[{ required: false, message: '请填写其他说明' }]"/>
      <van-button round block type="info" native-type="submit">提交</van-button>
    </van-form>
  </div>
</template>

<script>
  import { insert,update } from "@/api/common";
  import { Toast } from 'vant';

  export default {
    props:['user'],
    data() {
      return {
        items: [],
        role: 0,
        state: 0,
        userOj:{},
        roles: [
          { text: '全部角色', value: 0 },
          { text: '志愿者', value: 10 },
          { text: '酒店人员', value: 11 },
          { text: '求助者', value: 12 },
          { text: '管理人员', value: 13 }],
        states: [
          { text: '全部状态', value: 0 },
          { text: '已核实', value: 2 },
          { text: '未知', value: 1 },
          { text: '禁用', value: 3 },],
      }
    },
    watch: {
      user: {
        handler(newValue, oldValue) {
          this.role = newValue.rid;
          this.state = newValue.state;
          this.userOj = newValue;
        }
      }
    },
    mounted() {
    },
    methods: {   
      onSubmit(values) {
        if(this.role==0||this.state<=0) {
          Toast('请选择角色和状态');
          return;
        }
        this.userOj.rid = this.role;
        this.userOj.state = this.state;
        var params={
          'Eova_user': this.userOj,
          'tag': 'Eova_user'
        }
        if(this.userOj.id>0) {
          update(params)
            .then((res) => {
              if(res.data.msg=='success') {
                this.$emit('onAdded','');
                Toast.success('保存用户成功');
              }
              else
                Toast.fail('保存用户失败');
            })
            .catch(function (error){
                Toast.fail('调用服务器，保存用户失败');
            });
        }
        else {
          insert(params)
            .then((res) => {
              if(res.data.msg=='success') {
                this.$emit('onAdded','');
                Toast.success('保存用户成功');
              }
              else
                Toast.fail('保存用户失败');
            })
            .catch(function (error){
                Toast.fail('调用服务器，保存用户失败');
            });
        }
      },
    }
  }
</script>

<style scoped>

</style>
  